/*
 * Magic_ToolboxView.java
 */

package magic_toolbox;

import javax.swing.JComboBox;
import utilities.FileScanner;
import formatconversion_model.*;
import java.awt.FontMetrics;
import javax.swing.filechooser.FileFilter;
import org.jdesktop.application.Action;
import org.jdesktop.application.ResourceMap;
import org.jdesktop.application.SingleFrameApplication;
import org.jdesktop.application.FrameView;
import org.jdesktop.application.TaskMonitor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.Arrays;
import javax.swing.Timer;
import javax.swing.Icon;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import org.jdesktop.beansbinding.Converter;
import magic_model.*;


/**
 * The application's main frame.
 */
public class Magic_ToolboxView extends FrameView {
    private FileFilter magicFormatFilter = new MagicFormatFilter(this); //accept only certain data types
    private File parseFile=null;
   // private DefaultComboBoxModel parseBoxModel;
    private Converter inputOutputConverter = new InputToOutputFileNameConverter(this);
    private FileType inputFileType = FileType.none;
    
    public FileType getInputFileType(){
        return inputFileType;
    } 
    
    private MagicFormatParser getImportParser() {
       MagicFormatParser mp = null;
        switch(inputFileType){
            case cod: mp = new Cockatrice_Parser(); break;
            case mws: mp = new MWS_Parser(); break;
            case basic: mp = new Basic_Parser(); break;
            default: 
        }
        return mp;
    }
    
    

    public Magic_ToolboxView(SingleFrameApplication app) {
        super(app);

        
        initComponents();
        myInit();
        // status bar initialization - message timeout, idle icon and busy animation, etc
        ResourceMap resourceMap = getResourceMap();
        int messageTimeout = resourceMap.getInteger("StatusBar.messageTimeout");
        messageTimer = new Timer(messageTimeout, new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                statusMessageLabel.setText("");
            }
        });
        messageTimer.setRepeats(false);
        int busyAnimationRate = resourceMap.getInteger("StatusBar.busyAnimationRate");
        for (int i = 0; i < busyIcons.length; i++) {
            busyIcons[i] = resourceMap.getIcon("StatusBar.busyIcons[" + i + "]");
        }
        busyIconTimer = new Timer(busyAnimationRate, new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                busyIconIndex = (busyIconIndex + 1) % busyIcons.length;
                statusAnimationLabel.setIcon(busyIcons[busyIconIndex]);
            }
        });
        idleIcon = resourceMap.getIcon("StatusBar.idleIcon");
        statusAnimationLabel.setIcon(idleIcon);
        progressBar.setVisible(false);

        // connecting action tasks to status bar via TaskMonitor
        TaskMonitor taskMonitor = new TaskMonitor(getApplication().getContext());
        taskMonitor.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                String propertyName = evt.getPropertyName();
                if ("started".equals(propertyName)) {
                    if (!busyIconTimer.isRunning()) {
                        statusAnimationLabel.setIcon(busyIcons[0]);
                        busyIconIndex = 0;
                        busyIconTimer.start();
                    }
                    progressBar.setVisible(true);
                    progressBar.setIndeterminate(true);
                } else if ("done".equals(propertyName)) {
                    busyIconTimer.stop();
                    statusAnimationLabel.setIcon(idleIcon);
                    progressBar.setVisible(false);
                    progressBar.setValue(0);
                } else if ("message".equals(propertyName)) {
                    String text = (String)(evt.getNewValue());
                    statusMessageLabel.setText((text == null) ? "" : text);
                    messageTimer.restart();
                } else if ("progress".equals(propertyName)) {
                    int value = (Integer)(evt.getNewValue());
                    progressBar.setVisible(true);
                    progressBar.setIndeterminate(false);
                    progressBar.setValue(value);
                }
            }
        });
    }

    @Action
    public void showAboutBox() {
        if (aboutBox == null) {
            JFrame mainFrame = Magic_ToolboxApp.getApplication().getMainFrame();
            aboutBox = new Magic_ToolboxAboutBox(mainFrame);
            aboutBox.setLocationRelativeTo(mainFrame);
        }
        Magic_ToolboxApp.getApplication().show(aboutBox);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mainPanel = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        menuBar = new javax.swing.JMenuBar();
        javax.swing.JMenu fileMenu = new javax.swing.JMenu();
        javax.swing.JMenuItem exitMenuItem = new javax.swing.JMenuItem();
        jMenuItem1 = new javax.swing.JMenuItem();
        javax.swing.JMenu helpMenu = new javax.swing.JMenu();
        javax.swing.JMenuItem aboutMenuItem = new javax.swing.JMenuItem();
        jMenu3 = new javax.swing.JMenu();
        jMenuItem2 = new javax.swing.JMenuItem();
        statusPanel = new javax.swing.JPanel();
        javax.swing.JSeparator statusPanelSeparator = new javax.swing.JSeparator();
        statusMessageLabel = new javax.swing.JLabel();
        statusAnimationLabel = new javax.swing.JLabel();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenu2 = new javax.swing.JMenu();
        parseWindow = new javax.swing.JFrame();
        jLabel1 = new javax.swing.JLabel();
        openFileDialogButton = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        parserComboBox = new javax.swing.JComboBox();
        outputFileName = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        generateOutputfileButton = new javax.swing.JButton();
        invalidFileWarning = new javax.swing.JLabel();
        invalidFileWarning.setVisible(false);
        inputFileTextField = new javax.swing.JTextField();
        progressBar = new javax.swing.JProgressBar();
        jInputDialog = new javax.swing.JDialog();
        myInputFileChooser = new javax.swing.JFileChooser();
        myInputFileChooser.setVisible(false);
        jOutputDialog = new javax.swing.JDialog();
        myOutputFileChooser = new javax.swing.JFileChooser();

        mainPanel.setName("mainPanel"); // NOI18N

        jPanel1.setName("jPanel1"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(magic_toolbox.Magic_ToolboxApp.class).getContext().getActionMap(Magic_ToolboxView.class, this);
        jButton1.setAction(actionMap.get("testParse")); // NOI18N
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(magic_toolbox.Magic_ToolboxApp.class).getContext().getResourceMap(Magic_ToolboxView.class);
        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N

        jButton2.setText(resourceMap.getString("jButton2.text")); // NOI18N
        jButton2.setName("jButton2"); // NOI18N

        jButton3.setText(resourceMap.getString("jButton3.text")); // NOI18N
        jButton3.setName("jButton3"); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButton1)
                    .addComponent(jButton2)
                    .addComponent(jButton3))
                .addContainerGap(243, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(23, 23, 23)
                .addComponent(jButton1)
                .addGap(18, 18, 18)
                .addComponent(jButton2)
                .addGap(18, 18, 18)
                .addComponent(jButton3)
                .addContainerGap(140, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
        mainPanel.setLayout(mainPanelLayout);
        mainPanelLayout.setHorizontalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        mainPanelLayout.setVerticalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        menuBar.setName("menuBar"); // NOI18N

        fileMenu.setText(resourceMap.getString("fileMenu.text")); // NOI18N
        fileMenu.setName("fileMenu"); // NOI18N

        exitMenuItem.setAction(actionMap.get("quit")); // NOI18N
        exitMenuItem.setName("exitMenuItem"); // NOI18N
        fileMenu.add(exitMenuItem);

        jMenuItem1.setText(resourceMap.getString("jMenuItem1.text")); // NOI18N
        jMenuItem1.setName("jMenuItem1"); // NOI18N
        fileMenu.add(jMenuItem1);

        menuBar.add(fileMenu);

        helpMenu.setText(resourceMap.getString("helpMenu.text")); // NOI18N
        helpMenu.setName("helpMenu"); // NOI18N

        aboutMenuItem.setAction(actionMap.get("showAboutBox")); // NOI18N
        aboutMenuItem.setName("aboutMenuItem"); // NOI18N
        helpMenu.add(aboutMenuItem);

        menuBar.add(helpMenu);

        jMenu3.setText(resourceMap.getString("jMenu3.text")); // NOI18N
        jMenu3.setName("jMenu3"); // NOI18N

        jMenuItem2.setText(resourceMap.getString("jMenuItem2.text")); // NOI18N
        jMenuItem2.setToolTipText(resourceMap.getString("jMenuItem2.toolTipText")); // NOI18N
        jMenuItem2.setName("jMenuItem2"); // NOI18N
        jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem2ActionPerformed(evt);
            }
        });
        jMenu3.add(jMenuItem2);

        menuBar.add(jMenu3);

        statusPanel.setName("statusPanel"); // NOI18N

        statusPanelSeparator.setName("statusPanelSeparator"); // NOI18N

        statusMessageLabel.setName("statusMessageLabel"); // NOI18N

        statusAnimationLabel.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        statusAnimationLabel.setName("statusAnimationLabel"); // NOI18N

        javax.swing.GroupLayout statusPanelLayout = new javax.swing.GroupLayout(statusPanel);
        statusPanel.setLayout(statusPanelLayout);
        statusPanelLayout.setHorizontalGroup(
            statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(statusPanelSeparator, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
            .addGroup(statusPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(statusMessageLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 380, Short.MAX_VALUE)
                .addComponent(statusAnimationLabel)
                .addContainerGap())
        );
        statusPanelLayout.setVerticalGroup(
            statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(statusPanelLayout.createSequentialGroup()
                .addComponent(statusPanelSeparator, javax.swing.GroupLayout.PREFERRED_SIZE, 2, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(statusMessageLabel)
                    .addComponent(statusAnimationLabel))
                .addGap(3, 3, 3))
        );

        jMenuBar1.setName("jMenuBar1"); // NOI18N

        jMenu1.setText(resourceMap.getString("jMenu1.text")); // NOI18N
        jMenu1.setName("jMenu1"); // NOI18N
        jMenuBar1.add(jMenu1);

        jMenu2.setText(resourceMap.getString("jMenu2.text")); // NOI18N
        jMenu2.setName("jMenu2"); // NOI18N
        jMenuBar1.add(jMenu2);

        parseWindow.setTitle(resourceMap.getString("parseWindow.title")); // NOI18N
        parseWindow.setLocationByPlatform(true);
        parseWindow.setName("parseWindow"); // NOI18N

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        openFileDialogButton.setAction(actionMap.get("openLoadFileDialog")); // NOI18N
        openFileDialogButton.setText(resourceMap.getString("openFileDialogButton.text")); // NOI18N
        openFileDialogButton.setName("openFileDialogButton"); // NOI18N
        openFileDialogButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                openFileDialogButtonActionPerformed(evt);
                openFileDialogButtonActionPerformed1(evt);
                openFileDialogButtonActionPerformed2(evt);
            }
        });

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        parserComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Blub", "Blob", "Item 4" }));
        parserComboBox.setEnabled(false);
        parserComboBox.setName("parserComboBox"); // NOI18N
        parserComboBox.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                parserComboBoxItemStateChanged(evt);
            }
        });

        outputFileName.setText(resourceMap.getString("outputFileName.text")); // NOI18N
        outputFileName.setName("outputFileName"); // NOI18N

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        generateOutputfileButton.setText(resourceMap.getString("generateOutputfileButton.text")); // NOI18N
        generateOutputfileButton.setName("generateOutputfileButton"); // NOI18N
        generateOutputfileButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                generateOutputfileButtonActionPerformed(evt);
            }
        });

        invalidFileWarning.setForeground(resourceMap.getColor("invalidFileWarning.foreground")); // NOI18N
        invalidFileWarning.setText(resourceMap.getString("invalidFileWarning.text")); // NOI18N
        invalidFileWarning.setName("invalidFileWarning"); // NOI18N

        inputFileTextField.setText(resourceMap.getString("inputFileTextField.text")); // NOI18N
        inputFileTextField.setName("inputFileTextField"); // NOI18N

        javax.swing.GroupLayout parseWindowLayout = new javax.swing.GroupLayout(parseWindow.getContentPane());
        parseWindow.getContentPane().setLayout(parseWindowLayout);
        parseWindowLayout.setHorizontalGroup(
            parseWindowLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(parseWindowLayout.createSequentialGroup()
                .addGap(24, 24, 24)
                .addGroup(parseWindowLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(inputFileTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 179, Short.MAX_VALUE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(parseWindowLayout.createSequentialGroup()
                        .addComponent(openFileDialogButton)
                        .addGap(18, 18, 18)
                        .addComponent(invalidFileWarning)))
                .addGap(92, 92, 92)
                .addGroup(parseWindowLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(parserComboBox, 0, 122, Short.MAX_VALUE)
                    .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(97, 97, 97)
                .addGroup(parseWindowLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(parseWindowLayout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addContainerGap())
                    .addGroup(parseWindowLayout.createSequentialGroup()
                        .addGroup(parseWindowLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(outputFileName, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 198, Short.MAX_VALUE)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, parseWindowLayout.createSequentialGroup()
                                .addGap(121, 121, 121)
                                .addComponent(generateOutputfileButton)))
                        .addGap(59, 59, 59))))
        );
        parseWindowLayout.setVerticalGroup(
            parseWindowLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(parseWindowLayout.createSequentialGroup()
                .addGap(78, 78, 78)
                .addGroup(parseWindowLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(parseWindowLayout.createSequentialGroup()
                        .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(parserComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())
                    .addGroup(parseWindowLayout.createSequentialGroup()
                        .addGroup(parseWindowLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(jLabel3))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(parseWindowLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(outputFileName, javax.swing.GroupLayout.DEFAULT_SIZE, 23, Short.MAX_VALUE)
                            .addComponent(inputFileTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(parseWindowLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(generateOutputfileButton, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, parseWindowLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(openFileDialogButton)
                                .addComponent(invalidFileWarning)))
                        .addGap(169, 169, 169))))
        );

        parseWindow.getAccessibleContext().setAccessibleName(resourceMap.getString("jFrame1.AccessibleContext.accessibleName")); // NOI18N
        parseWindow.getAccessibleContext().setAccessibleDescription(resourceMap.getString("jFrame1.AccessibleContext.accessibleDescription")); // NOI18N
        parseWindow.getAccessibleContext().setAccessibleParent(jButton1);

        progressBar.setName("progressBar"); // NOI18N

        jInputDialog.setName("jInputDialog"); // NOI18N

        myInputFileChooser.setAcceptAllFileFilterUsed(false);
        myInputFileChooser.setCurrentDirectory(new java.io.File("C:\\Dropbox\\My Dropbox\\Magic"));
        myInputFileChooser.setDialogTitle(resourceMap.getString("myInputFileChooser.dialogTitle")); // NOI18N
        myInputFileChooser.setFileFilter(this.magicFormatFilter);
        myInputFileChooser.setDragEnabled(true);
        myInputFileChooser.setName("myInputFileChooser"); // NOI18N
        myInputFileChooser.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                myInputFileChooserActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jInputDialogLayout = new javax.swing.GroupLayout(jInputDialog.getContentPane());
        jInputDialog.getContentPane().setLayout(jInputDialogLayout);
        jInputDialogLayout.setHorizontalGroup(
            jInputDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 501, Short.MAX_VALUE)
            .addGroup(jInputDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jInputDialogLayout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(myInputFileChooser, javax.swing.GroupLayout.PREFERRED_SIZE, 481, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
        );
        jInputDialogLayout.setVerticalGroup(
            jInputDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
            .addGroup(jInputDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jInputDialogLayout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(myInputFileChooser, javax.swing.GroupLayout.DEFAULT_SIZE, 278, Short.MAX_VALUE)
                    .addContainerGap()))
        );

        jOutputDialog.setName("jOutputDialog"); // NOI18N

        myOutputFileChooser.setAcceptAllFileFilterUsed(false);
        myOutputFileChooser.setCurrentDirectory(new java.io.File("C:\\Dropbox\\My Dropbox\\Magic"));
        myOutputFileChooser.setDialogType(javax.swing.JFileChooser.SAVE_DIALOG);
        myOutputFileChooser.setName("myOutputFileChooser"); // NOI18N
        myOutputFileChooser.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                myOutputFileChooserActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jOutputDialogLayout = new javax.swing.GroupLayout(jOutputDialog.getContentPane());
        jOutputDialog.getContentPane().setLayout(jOutputDialogLayout);
        jOutputDialogLayout.setHorizontalGroup(
            jOutputDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jOutputDialogLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(myOutputFileChooser, javax.swing.GroupLayout.PREFERRED_SIZE, 508, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jOutputDialogLayout.setVerticalGroup(
            jOutputDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jOutputDialogLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(myOutputFileChooser, javax.swing.GroupLayout.PREFERRED_SIZE, 335, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        setComponent(mainPanel);
        setMenuBar(menuBar);
        setStatusBar(statusPanel);
    }// </editor-fold>//GEN-END:initComponents

    public JComboBox getParserComboBox() {
        return parserComboBox;
    }

    private void myInit(){
     //   parseBoxModel = new DefaultComboBoxModel();
//        BufferedImage myPicture;
//        try {
//            myPicture = ImageIO.read(new File("D:\\Workspaceeee\\magic-toolbox\\src\\utilities\\MTGLogo.jpg"));
//            JLabel picLabel = new JLabel(new ImageIcon( myPicture ));
//            JFrame mainFrame = Magic_ToolboxApp.getApplication().getMainFrame();
//           mainFrame.add( picLabel );
//        } catch (IOException ex) {
//            Logger.getLogger(Magic_ToolboxView.class.getName()).log(Level.SEVERE, null, ex);
//        }
       
        fillParserComboBox();
    }
    
    //set inputfile type from FileFilter
    public void setInputDataType(FileType inputFileType){
        this.inputFileType=inputFileType;
    }
    
    private void fillParserComboBox(){
       // JOptionPane.showMessageDialog(null, "filling combo box "+inputFileType);
        parserComboBox.removeAllItems();
        parserComboBox.setEnabled(true);
        switch(inputFileType){
            case cod: 
                parserComboBox.addItem(new MWS_Parser());
                parserComboBox.addItem(new Basic_Parser());
                break;
            case mws: 
                parserComboBox.addItem(new Cockatrice_Parser());
                parserComboBox.addItem(new Basic_Parser());
                break;
            case basic: 
                parserComboBox.addItem(new MWS_Parser());
                parserComboBox.addItem(new Cockatrice_Parser());
                break;
            case none:
            default: parserComboBox.setEnabled(false);
                
        }
    }
    
    private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem2ActionPerformed
       JOptionPane.showMessageDialog(null, "My Goodness, this is so concise");
    }//GEN-LAST:event_jMenuItem2ActionPerformed

    /*
    private void setFileType(String dateiEndung){
        //magicFormatFilter.getInputFileType(); // geht nicht!
    }*/
    
    private void myInputFileChooserActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_myInputFileChooserActionPerformed
        // TODO add your handling code here:
        if(evt.getActionCommand().equals("CancelSelection")){
          jInputDialog.setVisible(false);
          myInputFileChooser.setVisible(false);
          return;
        }
        
          File f = myInputFileChooser.getSelectedFile();
          
          this.parseFile = f;
          inputFileTextField.setText(f.getAbsolutePath());
          setOutputFileName();
          
          //set input type
          setInputType();
          
          //Fill OptionBox
          fillParserComboBox(); //lager das in nen EventListener aus
          
          
          //resize the textfield
            FontMetrics fm = inputFileTextField.getFontMetrics(inputFileTextField.getFont());
            int textwidth = fm.stringWidth(inputFileTextField.getText()+5);
            inputFileTextField.setSize(textwidth+5,inputFileTextField.getHeight());
        jInputDialog.setVisible(false);
        myInputFileChooser.setVisible(false);
        
        if(inputFileType!=FileType.none){
            invalidFileWarning.setVisible(false);
            generateOutputfileButton.setVisible(true);
            generateOutputfileButton.requestFocusInWindow();
        }else{
            invalidFileWarning.setVisible(true);
            generateOutputfileButton.setVisible(false);
        }
    }//GEN-LAST:event_myInputFileChooserActionPerformed

    private void setInputType(){
         int indexOFP = parseFile.getName().lastIndexOf(".");
          if(indexOFP == -1)throw new RuntimeException ("input file has no type");
          String dateiEndung = parseFile.getName().substring(indexOFP+1);
           if(dateiEndung.equals("mws") || dateiEndung.equals("mwDeck")|| dateiEndung.equals(".dec")){
            inputFileType= FileType.mws;
        }else if(dateiEndung.equals("cod")){
            inputFileType= FileType.cod;
        }else if(dateiEndung.equals("txt")||dateiEndung.equals("")){
            inputFileType= FileType.basic;
    //    }else if(dateiEndung.equals("dec")){
    //        inputFileType= inputFileType.dec;
        }else{
            inputFileType= FileType.none;
        }
        this.setInputDataType(inputFileType);
    }
    private void openFileDialogButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_openFileDialogButtonActionPerformed
        // TODO add your handling code here:
       
    }//GEN-LAST:event_openFileDialogButtonActionPerformed

    private void openFileDialogButtonActionPerformed1(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_openFileDialogButtonActionPerformed1
     //   jFileChooser1.setVisible(true);asdasd
    }//GEN-LAST:event_openFileDialogButtonActionPerformed1

    private void openFileDialogButtonActionPerformed2(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_openFileDialogButtonActionPerformed2
         jInputDialog.setVisible(true);
        myInputFileChooser.setVisible(true);
       
        /* klappt nicht =(
        int rueckgabeWert =  jFileChooser1.showOpenDialog(null);
       
          if(rueckgabeWert == JFileChooser.APPROVE_OPTION)
        {
             // Ausgabe der ausgewaehlten Datei
            System.out.println("Die zu öffnende Datei ist: " +
                  jFileChooser1.getSelectedFile().getName());
        }
          */
    }//GEN-LAST:event_openFileDialogButtonActionPerformed2

    private void generateOutputfileButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_generateOutputfileButtonActionPerformed
   
            // read input file
              //checkFile(parseFile);
        if(parseFile==null){
            JOptionPane.showMessageDialog(null, "No File selected");
        }else{
                    // JOptionPane.showMessageDialog(null, parseFile.getName());
        
            FileScanner in = new FileScanner(parseFile,true);
            String[] content = in.getContent();
           in.finish();

           //parseFile.getFileType();
           
            MagicFormatParser importParser = getImportParser();

            Deck deck = importParser.importFile(content); 
            if(deck.isEmpty()){
                throw new RuntimeException("Deck doesnt contain any cards");
            }
            // convert Content using parser
            MagicFormatParser selectedParser =(MagicFormatParser) parserComboBox.getSelectedItem();
                    //JOptionPane.showMessageDialog(null, selectedParser);
            
            String[] outputContent = selectedParser.exportFile(deck); 
            System.out.println(Arrays.toString(outputContent));
            
           
            //generate outputfile
            //TODO
            String outputname = outputFileName.getText();
            
            try{
                File outputFile = new File(outputname);
                FileScanner out = new FileScanner(outputFile,false);
                out.writeContent(outputContent);
                out.finish();
                JOptionPane.showMessageDialog(null, "File successfully generated");
            }catch(Exception e){
                JOptionPane.showMessageDialog(null, "An Error occured. The File was not created, because: " +e.getMessage());
                System.out.println(e.getMessage());
            }
             
             
        }
    }//GEN-LAST:event_generateOutputfileButtonActionPerformed

    private void parserComboBoxItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_parserComboBoxItemStateChanged
        // TODO add your handling code here:
        if(evt.getItem().equals(parserComboBox.getSelectedItem())){
            setOutputFileName();
        }
    }//GEN-LAST:event_parserComboBoxItemStateChanged
    private void myOutputFileChooserActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_myOutputFileChooserActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_myOutputFileChooserActionPerformed

    private void setOutputFileName(){
        outputFileName.setText((String)inputOutputConverter.convertForward(inputFileTextField.getText()));
    }
    
    
    @Action
    public void testParse() {
     //     JOptionPane.showMessageDialog(null, "Opening new window");
        /*
                if (jFrame1 == null) {
            JFrame mainFrame = Magic_ToolboxApp.getApplication().getMainFrame();
            aboutBox = new Magic_ToolboxAboutBox(mainFrame);
            aboutBox.setLocationRelativeTo(mainFrame);
        }
        Magic_ToolboxApp.getApplication().show(aboutBox);
         
         */
          //öffne ein neues Fenster
    //newFrame = new JFrame("New Window");
    parseWindow.pack();
    parseWindow.setVisible(true);
    openFileDialogButton.requestFocusInWindow();
    }

    
    @Action
    public void openLoadFileDialog() {
       // JOptionPane.showMessageDialog(null, "Blub");
         jInputDialog.pack();
        jInputDialog.setVisible(true);
    }
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton generateOutputfileButton;
    private javax.swing.JTextField inputFileTextField;
    private javax.swing.JLabel invalidFileWarning;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JDialog jInputDialog;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JDialog jOutputDialog;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JFileChooser myInputFileChooser;
    private javax.swing.JFileChooser myOutputFileChooser;
    private javax.swing.JButton openFileDialogButton;
    private javax.swing.JTextField outputFileName;
    private javax.swing.JFrame parseWindow;
    private javax.swing.JComboBox parserComboBox;
    private javax.swing.JProgressBar progressBar;
    private javax.swing.JLabel statusAnimationLabel;
    private javax.swing.JLabel statusMessageLabel;
    private javax.swing.JPanel statusPanel;
    // End of variables declaration//GEN-END:variables

    private final Timer messageTimer;
    private final Timer busyIconTimer;
    private final Icon idleIcon;
    private final Icon[] busyIcons = new Icon[15];
    private int busyIconIndex = 0;

    private JDialog aboutBox;
}
